#include <stdio.h>

int main(int argc, char *argv[])
{
    char *read_file = argv[1];

    int line_num = 0;
    FILE *fp;
    int read_char;

    fp = fopen(read_file, "r");

    if (fp == NULL) {
        printf("请输入有效的文件路径\n");
        perror("fopen");
        return -1;
    }

    while(1)
    {
        read_char = fgetc(fp);

        if (read_char == EOF) {
            if (feof(fp)) {
                printf("to the end.\n");
                break;
            } else {
                printf("fgetc error.\n");
                return -1;
            }
        }

        if (read_char == '\n') {
            line_num++;
        }
    }

    printf("There are %d lines.\n", line_num);

    if (fclose(fp) == EOF) {
        perror("fclose");
        return EOF;
    }

    return 0;
}
